package cn.cxw.components;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.util.AttributeSet;
import android.util.Log;

import com.baidu.mapapi.map.BaiduMap;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.cxw.core.Constants;
import cn.cxw.model.GaussPoint;
import cn.cxw.model.MapRect;


/**
 * 自定义的ImageView，用于绘制坐标网格
 *
 * @author guolin
 */
public class CoordGridView extends android.support.v7.widget.AppCompatImageView {

    private static String TAG;
    public int w;
    public int h;
    private Map<Integer, Point> leftPoints;
    private Map<Integer, Point> rightPoints;
    private Map<Integer, Point> topPoints;
    private Map<Integer, Point> bottomPoints;
    //四个角高斯坐标
    private GaussPoint nw, ne, sw, se;
    private static final int GRID_DISTANCE = 80;



	public CoordGridView(Context context, AttributeSet attrs) {
		super(context, attrs);
        TAG = this.getClass().getSimpleName();
	}


	@Override
	protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
		super.onLayout(changed, left, top, right, bottom);
		if (changed) {
			// 分别获取到ZoomImageView的宽度和高度
			w = getWidth();
			h = getHeight();
		}
	}

	//刷新
	public void refresh(MapRect mapRect, BaiduMap mBaiduMap){
        nw = mapRect.nw.gauss;
        ne = mapRect.ne.gauss;
        sw = mapRect.sw.gauss;
        se = mapRect.se.gauss;

        getGridPoints(mBaiduMap);
        invalidate();
    }

    //得到网格点的集合
    private void getGridPoints(BaiduMap mBaiduMap) {
        getLeftPoints(mBaiduMap);
        getRightPoints(mBaiduMap);
        getTopPoints(mBaiduMap);
        getBottomPoints(mBaiduMap);
    }

    //得到左边点集
    private void getLeftPoints(BaiduMap mBaiduMap){
        leftPoints = new HashMap<Integer, Point>();
        int y = (int) (nw.y / 1000) - 1;
        int startX = (int) (nw.x / 1000) + 1;
        int endX = (int) (sw.x / 1000);

        for(int x = startX; x >= endX; x--){
            cn.cxw.model.Point p = new cn.cxw.model.Point(new GaussPoint(x * 1000 + Constants.dp.x, y * 1000 + Constants.dp.y));
            Point point = mBaiduMap.getProjection().toScreenLocation(p.bd);
            leftPoints.put(x, point);
        }
    }

    //得到右侧点集
    private void getRightPoints(BaiduMap mBaiduMap){
        rightPoints = new HashMap<Integer, Point>();
        int y = (int) (ne.y / 1000) + 1;
        int startX = (int) (ne.x / 1000) + 1;
        int endX = (int) (se.x / 1000);

        for(int x = startX; x >= endX; x--){
            cn.cxw.model.Point p = new cn.cxw.model.Point(new GaussPoint(x * 1000 + Constants.dp.x, y * 1000 + Constants.dp.y));
            Point point = mBaiduMap.getProjection().toScreenLocation(p.bd);
            rightPoints.put(x, point);
        }
    }


    //得到顶部点集
    private void getTopPoints(BaiduMap mBaiduMap){
        topPoints = new HashMap<Integer, Point>();
        int x = (int) (ne.x / 1000) + 1;
        int startY = (int) (nw.y / 1000);
        int endY = (int) (ne.y / 1000) + 1;

        for(int y = startY; y <= endY; y++){
            cn.cxw.model.Point p = new cn.cxw.model.Point(new GaussPoint(x * 1000 + Constants.dp.x, y * 1000 + Constants.dp.y));
            Point point = mBaiduMap.getProjection().toScreenLocation(p.bd);
            topPoints.put(y, point);
        }
    }

    //得到底部点集
    private void getBottomPoints(BaiduMap mBaiduMap){
        bottomPoints = new HashMap<Integer, Point>();
        int x = (int) (se.x / 1000);
        int startY = (int) (sw.y / 1000);
        int endY = (int) (se.y / 1000) + 1;

        for(int y = startY; y <= endY; y++){
            cn.cxw.model.Point p = new cn.cxw.model.Point(new GaussPoint(x * 1000 + Constants.dp.x, y * 1000 + Constants.dp.y));
            Point point = mBaiduMap.getProjection().toScreenLocation(p.bd);
            bottomPoints.put(y, point);
        }
    }


    @Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
        Log.e(TAG, "重绘网格----------");
        Paint paint = new Paint();
        paint.setColor(Color.DKGRAY);                    //设置画笔颜色
        paint.setStrokeWidth((float) 2.0);              //设置线宽
        paint.setTextSize(40);
        paint.setTextAlign(Paint.Align.LEFT);
        drawXLine(canvas, paint);
        drawYLine(canvas, paint);
	}

	//绘制所有的X线
	private void drawXLine(Canvas canvas, Paint paint){
        if(leftPoints == null || rightPoints == null){
            Log.e(TAG, "X点集合为空");
            return;
        }

        Log.e(TAG, "leftPoints点个数为：" + leftPoints.size());
        Log.e(TAG, "rightPoints点个数为：" + rightPoints.size());
        int lastY = Integer.MIN_VALUE;
        List<Integer> list = sortMapWithKeys(leftPoints);
        for (Integer key : list) {
            Point leftPoint = leftPoints.get(key);
            Point rightPoint = rightPoints.get(key);
            if(rightPoint != null){
                Log.e(TAG, "Key:" + key);
                Log.e(TAG, "leftPoint：x_" + leftPoint.x + "    y_" + leftPoint.y);
                Log.e(TAG, "rightPoint：x_" + rightPoint.x + "    y_" + rightPoint.y);
                if(Math.abs(leftPoint.y - lastY) > GRID_DISTANCE) {
                    //绘制横线
                    canvas.drawLine(leftPoint.x, leftPoint.y, rightPoint.x, rightPoint.y, paint);
                    canvas.drawText((key % 100) + "", 10, leftPoint.y - 10, paint);
                    canvas.drawText((key % 100) + "", w - 50, rightPoint.y - 10, paint);
                    lastY = leftPoint.y;
                }
            }
        }
    }

    //绘制所有的Y线
    private void drawYLine(Canvas canvas, Paint paint){
        if(topPoints == null || bottomPoints == null){
            Log.e(TAG, "Y点集合为空");
            return;
        }
        Log.e(TAG, "topPoints点个数为：" + topPoints.size());
        Log.e(TAG, "bottomPoints点个数为：" + bottomPoints.size());
        int lastX = Integer.MIN_VALUE;
        List<Integer> list = sortMapWithKeys(topPoints);
        for (Integer key : list) {
            Point topPoint = topPoints.get(key);
            Point bottomPoint = bottomPoints.get(key);
            if(bottomPoint != null){
                Log.e(TAG, "Key:" + key);
                Log.e(TAG, "topPoint：x_" + topPoint.x + "    y_" + topPoint.y);
                Log.e(TAG, "bottomPoint：x_" + bottomPoint.x + "    y_" + bottomPoint.y);
                if(Math.abs(topPoint.x - lastX) > GRID_DISTANCE) {
                    canvas.drawLine(topPoint.x, topPoint.y, bottomPoint.x, bottomPoint.y, paint);
                    canvas.drawText((key % 100) + "", topPoint.x + 10, 40, paint);
                    canvas.drawText((key % 100) + "", bottomPoint.x + 15 , h - 10, paint);
                    lastX = topPoint.x;
                }
            }
        }
    }

    //按键值排序
    private List<Integer> sortMapWithKeys(Map<Integer, Point> map){
        List<Integer> list = new ArrayList<Integer>(map.keySet());
        //然后通过比较器来实现排序
        Collections.sort(list, new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o2.compareTo(o1);
            }
        });
        return list;
    }

}